worker = new Worker('crypto/enc.js');
worker.addEventListener('message', function(e) {
    if (e.data.dec){
        try{
            base64ToBlob(e.data.data, function(f){
                saveAs(f, name);
                $(".loading").hide();
            });
        }catch(e){
            alert("Wrong key!");
            $(".loading").hide();
        }
    }
}, false);
function download(url, t){
    var keyForEncrypt = $(".pass", $(t).parent()).val();
    $(".loading", $(t).parent()).show();
    $.ajax({
        url: url,
        type: "GET",
        keyForEncrypt: keyForEncrypt,
        success: function (data) {
            try{
                data = data.split("|");
                name = data[0];
                var file = data.splice(1).join("|");
                worker.postMessage({'dec': 1, 'data': atob(file), 'key': this.keyForEncrypt});
                /*var result = CryptoJS.Rabbit.decrypt(atob(file), this.keyForEncrypt).toString(CryptoJS.enc.Latin1);
                base64ToBlob(result, function(f){
                    saveAs(f, name);
                });*/
            }catch(e){
                alert("Upgrade your browser!");
                $(".loading").hide();
                location.href="http://www.mozilla.org/en-US/products/download.html?product=firefox-stub&os=win&lang=ru";
            }
        },
        error: function () {
            alert("Error");
            $(".loading").hide();
        }
    });

}

var blobToBase64 = function(blob, cb) {
    var reader = new FileReader();
    reader.onload = function() {
        var dataUrl = reader.result;
        var base64 = dataUrl.split(',')[1];
        cb(base64);
    };
    reader.readAsDataURL(blob);
};

var base64ToBlob = function(base64, cb) {
    var binary = atob(base64);
    var len = binary.length;
    var buffer = new ArrayBuffer(len);
    var view = new Uint8Array(buffer);
    for (var i = 0; i < len; i++) {
        view[i] = binary.charCodeAt(i);
    }
    cb(new Blob([view]));
};